﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 虚数
{
    class Complex
    {
        double ip;
        double rp;

        public Complex(double ip,double rp)
        {
            this.ip = ip;
            this.rp = rp;
        }

        public Complex() { }

        public static Complex operator + (Complex c1 , Complex c2)
        {
            Complex c = new Complex();
            c.ip = c1.ip + c2.ip;
            c.rp = c1.rp + c2.rp;

            return c;
        }

        public static Complex operator - (Complex c1 , Complex c2)
        {
            Complex c = new Complex();
            c.ip = c1.ip - c2.ip;
            c.rp = c1.rp - c2.rp;

            return c;
        }

        public static Complex operator -(Complex c)
        {
            c.ip = -c.ip;
            c.rp = -c.rp;

            return c;
        }

        public static implicit operator Complex (string s)
        {
            s = s.Trim();
            Complex c = new Complex();

            if (s.IndexOf('i') == -1)
            {
                c.ip = double.Parse(s);
                c.rp = 0;
            }

            else
            {
                if (s.IndexOf('-', 1) != -1)
                {
                    s = s.TrimEnd('i');

                    string[] digits = s.Split('-');

                    c.ip = double.Parse(digits[0]);
                    c.rp = -(double.Parse(digits[1]));

                    //c.ip = double.Parse(s.Remove(s.IndexOf('-', 0)));
                    //c.rp = -(double.Parse(s.Substring(s.IndexOf("-", 0) - 1)));

                }
                
                else if(s.IndexOf('+', 1) != -1)
                {
                     s = s.TrimEnd('i');
                    string[] digits = s.Split('+');

                    c.ip = double.Parse(digits[0]);
                    c.rp = double.Parse(digits[1]);
                    //c.ip = double.Parse(s.Remove(s.IndexOf('+', 0)));
                    //c.rp = double.Parse(s.Substring(s.IndexOf("+", 0) - 1));
                }

                else
                {
                    s = s.TrimEnd('i');

                    c.ip = 0;
                    c.rp = double.Parse(s);
                }
                //string[] digits = s.Split('+', '-');


                //c.ip = double.Parse(digits[0]);
                //c.rp = double.Parse(digits[1]);

            }

            return c;
        }

        public void Show()
        {
            if(ip != 0)
            {
                if (rp > 0)
                    Console.WriteLine("{0} + {1}i", ip, rp);
                else if (rp < 0)
                    Console.WriteLine("{0} - {1}i", ip, -rp);
                else
                    Console.WriteLine("{0}", ip);
            }
            else
            {
                if (rp > 0)
                    Console.WriteLine("{0}i", rp);
                else if (rp < 0)
                    Console.WriteLine("- {0}i", -rp);
                else
                    Console.WriteLine("0");
            }
            
        }
    }
}
